﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FGUFW.Core;
using UnityEngine.UI;

namespace FGUFW.Play.StepGrid
{
 
    public class DefaultModeView : MonoBehaviour
    {
        public IDefaultModeInput DefaultModeInput;
        public GameObject StartBtn;
        public GameObject EndBtn;

        public void Init()
        {
            StartBtn.SetActive(true);
            EndBtn.SetActive(false);
            var gridViews = GetComponentsInChildren<GridView>();
            var grids = StepGridPlayManager.I.Module<DefaultModeModule>().Grids;
            float gameViewSize = (transform as RectTransform).rect.height;
            foreach (var item in gridViews)
            {
                if(item.Grid.y==grids[item.Grid.x])
                {
                    item.GetComponent<Image>().color = Color.black;
                }
                else
                {
                    item.GetComponent<Image>().color = Color.white;
                    item.GetComponent<Image>().raycastTarget=true;
                }
                var rect = item.transform as RectTransform;
                rect.anchoredPosition = new Vector2(item.Grid.y*rect.rect.width,gameViewSize+item.Grid.x*rect.rect.height);
            }
        }

        public void OnClickStart()
        {
            StartBtn.SetActive(false);
            DefaultModeInput?.OnClickStart();
            var gridViews = GetComponentsInChildren<GridView>();
            foreach (var item in gridViews)
            {
                item.enabled=true;
            }
        }

        public void ShowEndBtn()
        {
            EndBtn.transform.GetChild(0).GetComponent<Text>().text = "总分:"+StepGridPlayManager.I.Module<DefaultModeModule>().SuccCount;
            EndBtn.transform.GetChild(1).GetComponent<Text>().text = "最高分:"+PlayerPrefs.GetInt(DefaultModeModule.Config.MAX_NUMBER,0);
            EndBtn.SetActive(true);
        }

        public void OnClickEnd()
        {
            DefaultModeInput?.OnClickEnd();
        }
    }   
}
